﻿using KqAutoDesign.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace KqAutoDesign.Base
{
    internal class StringToImgConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string path = (string)value;
            if(string.IsNullOrEmpty(path)) 
                return null;
            else
            {
                var iconPath = Environment.CurrentDirectory + @"\Resources\Icons\";
                return new BitmapImage(new Uri(iconPath+path, UriKind.Absolute));
            }
                
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    internal class EnumToDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Enum enumValue)
            {
                return enumValue.GetDescription();
            }
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Enum.Parse(targetType, value.ToString());
        }
    }

    public class StringToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is string str)
            {
                if(str.Equals("true",StringComparison.OrdinalIgnoreCase))
                    return true;
                if(str.Equals("false", StringComparison.OrdinalIgnoreCase))
                    return false;
                else
                    return true;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool boolValue = (bool)value;
            return boolValue ? "true" : "false";
        }
    }

    public class StatuToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is WorkFlowStateEnum state)
            {
                switch (state)
                {
                    case WorkFlowStateEnum.NoPlan:
                        return new SolidColorBrush(Colors.White);
                    case WorkFlowStateEnum.NotStarted:
                        return new SolidColorBrush(Colors.LightYellow);
                    case WorkFlowStateEnum.Running:
                        return new SolidColorBrush(Colors.DeepPink);
                    case WorkFlowStateEnum.Suspended:
                        return new SolidColorBrush(Colors.DimGray);
                    case WorkFlowStateEnum.finished:
                        return new SolidColorBrush(Colors.LightGreen);
                    default:
                        return new SolidColorBrush(Colors.White);
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
